//
//  FKMyBalanceVC.m
//  WanTao
//
//  Created by Macbook Pro on 2019/3/28.
//  Copyright © 2019 Macbook Pro. All rights reserved.
//

#import "FKMyBalanceVC.h"
#import "FKBalanceItemCell.h"
#import "FKBalanceDetailVC.h"
#import "FKChargeTypeVC.h"
static NSString *const ID = @"FKBalanceItemCell";

@interface FKMyBalanceVC ()<UITableViewDataSource, UITableViewDelegate,UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, weak) UITableView *tableView;
//@property (nonatomic, weak) UIView *container;
//@property (nonatomic, weak) UIImageView *avatarView;
//@property (nonatomic, weak) UILabel *nameLabel;
@property (nonatomic, weak) UILabel *balanceLabel;
@property (nonatomic, weak) UITextField *chargeField;
@property (nonatomic, weak) UIView *footer;
@property (nonatomic, weak) UICollectionView *collectionView;
@property (nonatomic, strong) NSArray *datas;

@end

@implementation FKMyBalanceVC

- (NSArray *)datas
{
    if (!_datas) {
        _datas = [NSArray array];
    }
    return _datas;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的余额";
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    [self.view addSubview:tableView];
    self.tableView = tableView;
    tableView.dataSource = self;
    tableView.delegate = self;
    tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag | UIScrollViewKeyboardDismissModeInteractive;
    
    UIView *bottomView = [[UIView alloc] init];
    [self.view addSubview:bottomView];
    bottomView.backgroundColor = [UIColor whiteColor];
    
    UIButton *confirmBtn = [UIButton fk_btnWithTarget:self action:@selector(confirmBtnClicked) font:fk_adjustFont(16) normalTitle:@"确定" normalTitleColor:[UIColor whiteColor] selectedTitle:nil selectedTitleColor:nil normalImg:nil selectedImg:nil bgImg:[UIImage imageNamed:@"bg_main"] selectedBgImg:nil];
    [bottomView addSubview:confirmBtn];
    [confirmBtn fk_viewCornerRadius:5 borderWidth:0 borderColor:nil];

    // 头部
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, fkScreenW, fk_adjustW(195))];
    
    UIImageView *topBgView = [[UIImageView alloc] initWithFrame:header.bounds];
    [header addSubview:topBgView];
    topBgView.image = [UIImage imageNamed:@"qianbaobeijing"];
//    topBgView.contentMode = UIViewContentModeScaleToFill;
//    [topBgView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(redBgViewDidTap)]];
    
    UILabel *balanceTitle = [UILabel fk_labelWithFont:fk_adjustFont(14) textColor:[UIColor whiteColor] text:@"账户余额(元)"];
    [header addSubview:balanceTitle];
    
    UILabel *balanceLabel = [UILabel fk_labelWithFont:fk_adjustFont(24) textColor:[UIColor whiteColor] textAlignment:NSTextAlignmentCenter];
    [header addSubview:balanceLabel];
    self.balanceLabel = balanceLabel;
    
    UIButton *detailBtn = [UIButton fk_btnWithTarget:self action:@selector(detailBtnClicked) font:fk_adjustFont(14) normalTitle:@"余额明细" normalTitleColor:[UIColor whiteColor] selectedTitle:nil selectedTitleColor:nil normalImg:nil selectedImg:nil bgImg:nil selectedBgImg:nil];
    [header addSubview:detailBtn];
    [detailBtn fk_viewCornerRadius:5 borderWidth:0.8 borderColor:[UIColor whiteColor]];
    
    tableView.tableHeaderView = header;
    
    // 底部
    UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, fkScreenW, fk_adjustW(120))];
    footer.backgroundColor = [UIColor whiteColor];
    self.footer = footer;
    
    UILabel *chargeTitle = [UILabel fk_labelWithFont:fk_adjustFont(14) textColor:fkColor333333 text:@"充值金额:"];
    [footer addSubview:chargeTitle];
    
    UITextField *chargeField = [[UITextField alloc] init];
    [footer addSubview:chargeField];
    self.chargeField = chargeField;
    chargeField.font = fk_adjustFont(14);
    chargeField.placeholder = @"请输入充值金额，最低1元";
    chargeField.backgroundColor = fkBgColor;
    chargeField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 8, 8)];
    chargeField.leftViewMode = UITextFieldViewModeAlways;
    [chargeField fk_viewCornerRadius:5 borderWidth:0 borderColor:nil];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = fk_adjustW(14);
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    CGFloat itemW = (fkScreenW - 24 - 2 * fk_adjustW(20)) / 3.f;
    CGFloat itemH = fk_adjustW(67);
    layout.itemSize = CGSizeMake(itemW, itemH);
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    [footer addSubview:collectionView];
    self.collectionView = collectionView;
    collectionView.dataSource = self;
    collectionView.delegate = self;
    collectionView.backgroundColor = [UIColor whiteColor];
    [collectionView registerClass:[FKBalanceItemCell class] forCellWithReuseIdentifier:ID];
    
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.offset(0);
        make.height.offset(fk_adjustW(64));
    }];
    [confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(12);
        make.right.offset(-12);
        make.height.offset(fk_adjustW(44));
        make.centerY.offset(0);
    }];
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.offset(0);
        make.bottom.equalTo(bottomView.mas_top);
    }];
    [balanceTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(fk_adjustW(45));
        make.centerX.offset(0);
    }];
    [balanceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.offset(0);
        make.width.offset(fkScreenW - 24);
        make.top.equalTo(balanceTitle.mas_bottom).offset(fk_adjustW(26));
    }];
    [detailBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(fk_adjustW(70));
        make.height.offset(fk_adjustW(24));
        make.centerX.offset(0);
        make.bottom.offset(fk_adjustW(-15));
    }];
    [chargeTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(12);
        make.top.offset(0);
        make.height.offset(fk_adjustW(50));
    }];
    [chargeField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(fkScreenW - 24);
        make.centerX.offset(0);
        make.height.offset(fk_adjustW(44));
        make.top.equalTo(chargeTitle.mas_bottom);
    }];
    [collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.centerX.equalTo(chargeField);
        make.top.equalTo(chargeField.mas_bottom).offset(fk_adjustW(20));
        make.bottom.offset(0);
    }];
//    footer.frame = ; // 加1是为了给collectionView一个初始高度
    self.tableView.tableFooterView = footer;
    
    
    [self loadData];
}

- (void)confirmBtnClicked
{
    
}

- (void)detailBtnClicked
{
    
}

- (void)redBgViewDidTap
{
    FKBalanceDetailVC *vc = [[FKBalanceDetailVC alloc] init];
    fkPushNav(vc);
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 0;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.datas.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    FKBalanceItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    cell.data = self.datas[indexPath.item];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    FKChargeTypeVC *vc = [[FKChargeTypeVC alloc] init];
    vc.data = self.datas[indexPath.item];
    fkPushNav(vc);
}

#pragma mark - 网络请求
- (void)loadData
{
    [FKHttpTool postWithSuffix:@"m=Home&c=Mine&a=recharge_info" param:nil success:^(FKBaseResult *result) {
        if (result.isSuccess) {
            NSDictionary *member = result.data[@"member"];
//            [self.avatarView sd_setImageWithURL:[NSURL URLWithString:member[@"avatar"]] placeholderImage:kDefaultAvatar];
//            self.nameLabel.text = member[@"nickname"];
            self.balanceLabel.text = fkFORMAT(@"￥%@", member[@"balance"]);
            
            NSArray *list = result.data[@"list"];
            if (list.count > 0) {
                NSInteger count = list.count;
                NSInteger rows = count % 3 == 0 ? count / 3 : count / 3 + 1;
                CGFloat itemH = fk_adjustW(67);
                self.footer.fk_height = (fk_adjustW(120) + rows * (itemH + fk_adjustW(14)));
                self.tableView.tableFooterView = self.footer;
                self.datas = list;
                [self.collectionView reloadData];
            }
        }
    } failure:nil];
}

@end
